// sw.js
console.log('service worker 注册成功')

self.addEventListener('install', () => {
   // 跳过等待
    self.skipWaiting()
     e.waitUntil(
            caches.open('v1').then(cache => {
                return cache.addAll([
                    '/index.js',
                    '/index.html',
                    '/icons/logo.png',
                    '/'
                ]);
            })
        );
  // 安装回调的逻辑处理
  console.log('service worker 安装成功')
})

self.addEventListener('activate', () => {
  // 激活回调的逻辑处理
  console.log('service worker 激活成功')
})

self.addEventListener('fetch', function (event) {
    event.respondWith(
        caches.match(event.request)
        .then(function (response) {

            if (response) {
                return response;
            }

            var fetchRequest = event.request.clone();

            return fetch(fetchRequest).then(
                function (response) {

                    if (!response || response.status !== 200 || response.type !== 'basic') {
                        return response;
                    }

                    var responseToCache = response.clone();

                    caches.open('v1')
                        .then(function (cache) {
                            cache.put(event.request, responseToCache);
                        });

                    return response;
                }
            );
        })
    );
});
